//
//  SISSContractRightFillinUnitCell.m
//  SISSAPP
//
//  Created by 优房网 on 2017/8/25.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSContractRightFillinUnitCell.h"


@interface SISSContractRightFillinUnitCell ()<UITextFieldDelegate>

/// Label
@property (weak, nonatomic) UILabel *label;
/// textField
@property (weak, nonatomic) UITextField *textField;
/// unitLabel
@property (weak, nonatomic) UILabel *unitLabel;
/* bottomLine */
@property (weak, nonatomic) UIView *bottomLine;

@end
@implementation SISSContractRightFillinUnitCell

- (void)setContractModel:(SISSContractModel *)contractModel {
    [super setContractModel:contractModel];
    
    if ([[contractModel.leftStr substringToIndex:1] isEqualToString:@"*"]) {
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:contractModel.leftStr];
        [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,1)];
        self.label.attributedText = attributedStr;
    } else {
        self.label.text = contractModel.leftStr;
    }
    
    if (contractModel.leftStr) {
        NSDictionary *attrs = @{ NSFontAttributeName : [UIFont systemFontOfSize:16]};
        CGFloat width = [contractModel.leftStr boundingRectWithSize:self.label.bounds.size options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.width;
        if (width > 18.5 * 8) {
            [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView).offset(width + 16);
            }];
        } else {
            [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView).offset(160);
            }];
        }
    }
    
    self.textField.placeholder = contractModel.placeHolder;
    self.textField.text = contractModel.rightStr;
    self.unitLabel.text = contractModel.unit;
}


#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    self.contractModel.rightStr = textField.text;
    
    if (self.contractModel.isChecked && [self.delegate respondsToSelector:@selector(contractRightFillinUnitCellTextFieldDidEndEditing)]) {
        [self.delegate contractRightFillinUnitCellTextFieldDidEndEditing];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *str = [NSString stringWithFormat:@"%@%@",textField.text,string];
    if (str.length > 9) {
        [SISSTools showSVTitle:@"不超过9位数字" andTime:1 andColor:YES];
        return NO;
    }
    return YES;
}

#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0;
    label.textColor = MAIN_TEXT_COLOR;
    label.font = [UIFont systemFontOfSize:16];
    label.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:label];
    self.label = label;
    
    UITextField *textField = [[UITextField alloc] init];
    textField.textColor = MAIN_TEXT_COLOR;
    textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:SECONDARY_TEXT_COLOR}];
    textField.delegate = self;
    textField.keyboardType = UIKeyboardTypeDecimalPad;
    [self.contentView addSubview:textField];
    self.textField = textField;
    
    UILabel *unitLabel = [[UILabel alloc] init];
    unitLabel.numberOfLines = 0;
    unitLabel.textColor = MAIN_TEXT_COLOR;
    unitLabel.font = [UIFont boldSystemFontOfSize:16];
    unitLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:unitLabel];
    self.unitLabel = unitLabel;
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:bottomLine];
    self.bottomLine = bottomLine;

}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(weakSelf.contentView).offset(1.5 * margin);
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(20 * margin);
        make.centerY.equalTo(weakSelf.contentView);
        make.right.mas_greaterThanOrEqualTo(weakSelf.unitLabel.mas_left).offset(-margin).priority(999);
    }];
    
    [self.unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.label);
        make.right.equalTo(weakSelf.contentView).offset(-margin);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.label.mas_bottom).offset(1.5 * margin);
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
}






@end
